package com.navcoo.yidaaddproapi.controller;


import com.navcoo.yidaaddproapi.response.Result;
import com.navcoo.yidaaddproapi.service.FileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 文件控制器
 *
 * @author Navcoo
 * @date 2024-03-28
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/file")
public class FileController {

    private final FileService fileService;

    /**
     * 上传
     *
     * @param file 文件
     * @return {@link Result}<{@link String}>
     * @throws IOException              ioexception
     * @throws NoSuchAlgorithmException 没有这样算法例外
     * @throws InvalidKeyException      无效密钥异常
     */
    @RequestMapping("/upload")
    public Result<String> upload(@RequestPart MultipartFile file) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        log.info("上传文件：{}", file.getOriginalFilename());
        return fileService.upload(file);
    }

}
